package com.whfc.wxmp.api.emp.controller;

import com.whfc.common.result.*;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.emp.dto.AppEmpGpsDTO;
import com.whfc.emp.dto.WxEmpDataCenterDTO;
import com.whfc.emp.dto.WxEmpWorkHourDTO;
import com.whfc.emp.dto.WxMapEmpDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.emp.service.WxEmpService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description 人员数据接口
 * @date 2020-09-23
 */
@RestController
@RequestMapping("/mp/api/emp")
public class WxEmpDataController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxEmpService wxEmpService;

    @GetMapping("/gps/log")
    public Result gpsLog(@CurrentWxUser WxUser wxUser,
                         @RequestParam("empId") Integer empId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员轨迹", RequestConetxtUtil.getParameters());
        ListData<AppEmpGpsDTO> data = wxEmpService.gpsLog(empId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/gps/latest")
    public Result getLatestGps(@CurrentWxUser WxUser wxUser,
                               @RequestParam("empId") Integer empId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员当前位置", RequestConetxtUtil.getParameters());
        AppEmpGpsDTO data = wxEmpService.getLatestGps(empId);
        return ResultUtil.success(data);
    }

    @GetMapping("/data/center")
    public Result empDataCenter(@CurrentWxUser WxUser wxUser,
                                @RequestParam(value = "deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员数据中心", RequestConetxtUtil.getParameters());
        WxEmpDataCenterDTO wxEmpDataCenterDTO = wxEmpService.getEmpDataCenter(deptId);
        return ResultUtil.success(wxEmpDataCenterDTO);
    }

    @GetMapping("/attendRecord/list")
    public Result empWorkHour(@CurrentWxUser WxUser wxUser,
                              @RequestParam(value = "empId") Integer empId,
                              @RequestParam(value = "date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员进出记录", RequestConetxtUtil.getParameters());
        WxEmpWorkHourDTO data = wxEmpService.getEmpWorkHour(wxUser, empId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/map/list")
    public Result mapList(@CurrentWxUser WxUser wxUser,
                          @RequestParam(value = "deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询地图页人员列表", RequestConetxtUtil.getParameters());
        WxMapEmpDTO wxMapEmpDTO = wxEmpService.getMapList(deptId);
        return ResultUtil.success(wxMapEmpDTO);
    }

    /**
     * 2.0 地图人员列表
     * @param wxUser
     * @param deptId
     * @return
     */
    @GetMapping("/map/list/v2")
    public Result mapListNew(@CurrentWxUser WxUser wxUser,
                             @RequestParam(value = "deptId") Integer deptId,
                             @RequestParam(value = "groupIds",required = false) List<Integer> groupIds,
                             @RequestParam(value = "corpIds",required = false) List<Integer> corpIds,
                             @RequestParam(value = "name",required = false) String name) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询地图页人员列表", RequestConetxtUtil.getParameters());
        WxMapEmpDTO wxMapEmpDTO = wxEmpService.getMapListNew(deptId,groupIds,corpIds,name);
        return ResultUtil.success(wxMapEmpDTO);
    }


    /**
     *
     * @param empId
     * @param date
     * @return
     */
    @GetMapping("/gps/log/page")
    public R<PageVO<AppEmpGpsDTO>> gpsLog(
            @RequestParam("empId") Integer empId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}",
                RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员轨迹", RequestConetxtUtil.getParameters());
        PageVO<AppEmpGpsDTO> data = wxEmpService.gpsLogPage(empId, date, pageNum, pageSize);
        return R.success(data);
    }
}
